<?php
class Account_Controller extends AdminController {
    
    public function __construct() {
        parent::__construct('account');
        $this->setDefaultListOrder('last_name');
    }
    
    public function all() {
        $this->initPagination('account');
        
        $this->viewData['accountList'] = $this->dao->queryAll($this->orderLimitAndOffset(
                'SELECT p.*, c.name AS country_name ' .
                'FROM account AS p ' .
                'LEFT OUTER JOIN country AS c ON p.country_code = c.code'));
        
        $this->renderSubview('admin/account/accountList');
    }
    
    public function display() {
        $id = $this->input->get('id');

        if (! valid::isId($id)) {
            $this->redirectToList('error.invalidId');
        }
        
        $account = $this->dao->queryOne(
                'SELECT p.*, c.name AS country_name ' .
                'FROM account p ' .
                'LEFT OUTER JOIN country c ON p.country_code = c.code ' .
                'WHERE p.id = :id',
                array('id' => $id));
        if (! $account) {
            $this->redirectToList('error.recordNotFound');
        }
        $this->viewData['account'] = $account;
        $this->renderSubview('admin/account/accountDisplay');
    }
    
    public function edit() {
        $id = $this->input->get('id');
        $account = array();
        
        if ($id) {
            $account = $this->dao->getById('account', $id);
            if (! $account) {
                $this->redirectToList('error.recordNotFound');
            }
        }
        else {
            $account['is_admin'] = sql::$FALSE;
            // Set default insert values here
        }
        
        $this->loadFormData();
        
        $this->viewData['account'] = $account;
        $this->renderSubview('admin/account/accountEdit');
    }
    
    protected function loadFormData() {
        $this->viewData['countryOptions'] = $this->dao->optionsCountry();
    }
    
    public function save() {
        $account = $this->postToDbValues();
        $id = $account['id'];
        
        $this->viewData['account'] = $account;
        
        // Basic validation
        $validator = new EntityValidator($account, 'account', $this->msg);
        // $validator->required('login');
        // $validator->minLength('password', 5);
        $validator->required('first_name');
        $validator->required('last_name');
        $validator->required('gender');
        $validator->date('birth_date');
        $validator->required('country_code');
        $validator->required('email');
        
        if ($this->msg->hasErrors()) {
            $this->loadFormData();
            $this->renderSubview('admin/account/accountEdit');
            return;
        }

        // Database validation
        $uniqueFields = array ('first_name', 'last_name', 'birth_date');
        if ($this->dao->alreadyExists('account', $uniqueFields, $account)) {
            $this->loadFormData();
            $this->msg->error('account.error-alreadyExists-names');
            $this->renderSubview('admin/account/accountEdit');
            return;
        }

        if ($this->dao->alreadyExists('account', 'login', $account)) {
            $this->loadFormData();
            $this->msg->error('account.error-alreadyExists-login');
            $this->renderSubview('admin/account/accountEdit');
            return;
        }
        
        // Insert or update the seminar
        if ($id != null) {
            $this->dao->update('account', $account);
            $this->msg->info('account.editSuccess', $account['first_name'] . ' ' . $account['last_name']);
        }
        else {
            $id = $this->dao->insert('account', $account);
            $this->msg->info('account.createSuccess', $account['first_name'] . ' ' . $account['last_name']);
        }

        $this->redirectToList();
    }
    
    public function delete() {
        $id = $this->input->get('id');
        
        // Basic validation
        if (! valid::isId($id)) {
            $this->redirectToList('error.invalidId');
        }

        $this->dao->delete('account', $id);
        
        $this->msg->info('account.deleteSuccess');
        $this->redirectToList();
    }

    public function searchForm() {
        $this->contentLayout = 'admin/searchLayout';
        // $this->loadFormData();
        $this->viewData['searchForm'] = $this->getSessionData()->getSearchCriteria($this->formName);
        $this->renderSubview('admin/account/accountSearch');
    }

    public function search() {
        $searchForm = $this->postToDbValues();
        $this->getSessionData()->setSearchCriteria($this->formName, $searchForm);
        $this->redirectToList();
    }

    public function registrationList() {
        $id = $this->input->get('id');
        if (! valid::isId($id)) {
            $this->redirectToList('error.invalidId');
        }

        $account = $this->dao->getById('account', $id);
        if (! $account) {
            $this->redirectToList('error.recordNotFound');
        }
        $this->viewData['account'] = $account;

        $this->viewData['viRegistrationList'] = $this->dao->findViRegistrationsByPerson($id);
        $this->viewData['registrationList'] = $this->dao->findRegistrationsByPerson(
                $id, $this->getL10nLanguageCode());
        
        foreach ($this->viewData['viRegistrationList'] as &$viRegistration) {
            $viRegistration['paymentList'] = $this->dao->getByField('vi_registration_payment', 'vi_registration_id', $viRegistration['id'], 'payment_date ASC, payment_time ASC');
        }

        $this->renderMySubview('accountRegistrationList');
    }
}
